package cn.itsource.org.service.impl;

import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.util.PageList;
import cn.itsource.configur.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    /**
     * 注入mapper层
     *
     * @return
     */
    @Autowired
    public DepartmentMapper departmentMapper;


    @Override
    public List<Department> fainAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public Department FainOne(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.insertSelective(department);
        update(department);
    }


    /**
     * 修改
     * @param department
     */
    @Override
    public void update(Department department) {
        //创建一个StringBuilder类型 的,性能更加的高效,是可以改变的,用来进行字符串的凭借
        StringBuilder sb= new StringBuilder();

          //拿到前台传递的内容
        Long[] parentIds = department.getParentIds();
        //前台传递过来的关系数组 ,来判断他的长度,来确定是否为空
        if (parentIds.length>0){
            Long parentId = parentIds[parentIds.length - 1];

            department.setParentId(parentId);

            for (Long id: parentIds){
                sb.append("/").append(id);
            }
             // sb.append("/").append(department.getId());
            department.setDirPath(sb.append("/").append(department.getId()).toString());
        }

        departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }



    //分页并查询内容
    @Override
    public PageList<Department> findPage(DepartmentQuery departmentQuery) {

        //使用分页产检拿到页数和行数
        PageHelper.startPage(departmentQuery.getPage(),departmentQuery.getSize());
        //sql查询所有的内容
        List<Department> queries = departmentMapper.findPage(departmentQuery);
        //封装到pageinfo中
        PageInfo<Department> pageInfo = new PageInfo<>(queries);

        //将封装号的内容添加到PageList中传回到页面中
        PageList<Department> list = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        return list;
    }


    /**
     * 获得上级部门和下级部门的内容
     * @return
     */
    @Override
    public List<Department> getHigher() {
        //第一步.先拿到表中所有的内容
        List<Department> departments = departmentMapper.selectAll();
        //第二部.将拿到的所有内容,放入导入的无限极的判断式子中自己分类
        List<Department> tree = TreeUtil.getTree(departments);

        return tree;
    }

    @Override
    public void BatchDeletion(DepartmentQuery departmentQuery) {
        //拿到前端传递过来的要删除的集合类型的if
        Long[] ibs = departmentQuery.getIds();
        //遍历
        for(Long id:ibs){
            departmentMapper.deleteByPrimaryKey(id);
        }
    }

}
